package cn.com.gridinfo.sooc.core.my.setting.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.expand.score.activity.BindZFBActivity;
import cn.com.gridinfo.sooc.core.my.commen.CodeConst;
import cn.com.gridinfo.sooc.core.my.dao.MyDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 显示已绑定的账户
 * Created by liuxiaochen on 2015/12/24.
 */
public class ShowAccountActivity extends BaseSoocActivity {
    MyDao myDao;
    @InjectView(R.id.myAccount)//显示绑定的账户
            TextView myAccount;
    @InjectView(R.id.boundAccount)// 用来点击
            RelativeLayout boundAccount;
    @InjectView(R.id.jiantou)//箭头 是否显示
            ImageView jiantou;
    @InjectView(R.id.qubangding)// 去绑定 是否显示
            TextView qubangding;
    private String onBoundZFB = "yes";//是否绑定了支付宝 yes：绑定了 no：没绑定

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_account_activity);
        ButterKnife.inject(this);
        myDao = new MyDao(this);
        myDao.myAccount("aliaccount");//请求接口

    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "提现账户";
    }

    //设置左边按钮
    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //返回
    @OnClick(R.id.toolbar_leftbtn)
    void onGoBack() {
        finish();

    }

    //请求成功返回数据
    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        if (myDao.status.equals("0")) {
            switch (requestCode) {
                case CodeConst.GETMYACCPUNT:
                    if (myDao.getAccountList() != null && myDao.getAccountList().size() > 0) {
                        onBoundZFB = "yes";//设置标识，绑定了支付宝
                        String show = myDao.getAccountList().get(0).getZh();
                        if (show.contains("@")) {
//                            if(show.length()>3){
//                                String beforeEmail = show.substring(0, 2);//拿到邮箱地址的前两位
//                                String emailTyp = show.substring(show.indexOf("@") + 1);//拿到邮箱类型
//                                showAccount = beforeEmail + "****@" + emailTyp;
//                            }else{
//                                showAccount = show;
//                            }
                        } else {
//                            String beforePhone = show.substring(0, 3);//拿到电话号码前3位
//                            String afterPhone = show.substring(7, 11);//拿到电话号码的后4位
//                            showAccount = beforePhone + "****" + afterPhone;
                        }
                        myAccount.setText(show);
                        jiantou.setVisibility(View.VISIBLE);
                        qubangding.setVisibility(View.GONE);
                    } else {
                        myAccount.setText("未绑定支付宝账号");
                        onBoundZFB = "no";//设置标识，没有绑定支付宝
                        myAccount.setTextColor(Color.LTGRAY);
                        jiantou.setVisibility(View.GONE);
                        qubangding.setVisibility(View.VISIBLE);
                    }
                    break;
            }
        }
    }

    // 点击进入下一步
    @OnClick(R.id.boundAccount)
    void setBoundAccount() {
        if (onBoundZFB.equals("yes")) {
            Intent intent = new Intent(this, BindZFBActivity.class);
            intent.putExtra("ZFB_ZH", myDao.getAccountList().get(0).getZh());//将支付宝的账户传出去
            intent.putExtra("ZFB_XM", myDao.getAccountList().get(0).getZhmc());//将支付宝的账户名称传出
            startActivityForResult(intent, 1);//绑定成功后刷新一下
//            finish();
            AnimUtil.intentSlidIn(this);
        } else if (onBoundZFB.equals("no")) {
            startActivityForResult(new Intent(this, BindZFBActivity.class), 1);
            //绑定成功后刷新一下
            AnimUtil.intentSlidIn(this);
        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                //绑定成功后刷新一下
                if (resultCode == RESULT_OK) {
                    myDao.myAccount("aliaccount");//请求接口
                }

                break;

        }
    }
}
